package leetcode;

import util.Tree;
import util.TreeNode;

/**
 * @Project : AlgorithmLearning
 * @Package : Leetcode
 * @File : Q0404.java
 * @Author : WangRuoyu
 * @Date 2023/11/11 20:54
 */


public class Q0404 {
    public int dfs(TreeNode root, boolean tag) {
        if (root.left == null && root.right == null) {
            return tag ? root.val : 0;
        }
        int res = 0;
        if (root.left != null) res += dfs(root.left, true);
        if (root.right != null) res += dfs(root.right, false);
        return res;
    }

    public int sumOfLeftLeaves(TreeNode root) {
        return dfs(root, false);
    }

    public static void main(String[] args) {
        Q0404 sl = new Q0404();
        String[] nodes = {"3", "9", null, null, "20", "15", null, null, "7", null, null};
        TreeNode root = Tree.createTreeByPreorder(nodes);
        System.out.println(sl.sumOfLeftLeaves(root));
    }
}